package cpr.context;

import cpr.Rule;
import cpr.data.HLVector;

public class PrintContext {
	private Context ctx;
	public PrintContext(Context ctx){
		this.ctx = ctx;
	}
	
	public String printRuleUsingDict(Rule aDigitalRule, HLVector vector) {
	    StringBuffer sb = new StringBuffer();
	    for (int h=0;h<Rule.HLIS.length;h++) {
	        int hlIndex = Rule.HLH[h][vector.getValue(h)];
	        int d = aDigitalRule.getHLValueDigital(hlIndex);
	        String key = ctx.getDictionary().getStringValueFor(d);
	        sb.append(key+" ");
	    }
	    return sb.toString();
	}
	
	public String printRuleAccordingToVectorOld(Rule aRule, HLVector vector) {
	    StringBuffer sb = new StringBuffer();
	    for (int h=0;h<Rule.HLIS.length;h++) {
	        int hlIndex = Rule.HLH[h][vector.getValue(h)];
	        String v = aRule.getHLValue(hlIndex);
	        sb.append(v+" ");
	    }
	    return sb.toString();
	}
	public String printRuleAccordingToVectorFullOld1(Rule aRule, HLVector vector) {
	    StringBuffer sb = new StringBuffer();
	    for (int h=0;h<Rule.HLIS.length;h++) {
	    	for (int j = 0; j < vector.getValue(h); j++) {
	    		int hlIndex = Rule.HLH[h][j+1];
	            String v = aRule.getHLValue(hlIndex);
	            sb.append(v+" ");
			}
	    }
	    return sb.toString();
	}
	public String getStringAttrValue(Rule aRule, boolean digitalRule) {
		if (digitalRule) {
			int d = aRule.attrValueDigital;
			String key = ctx.getDictionary().getStringValueFor(d);
	        return key;
		} else {
			return aRule.attrValue;
		}
	}
    
    public int getDigitalKeyValue(int h, int j, Rule aRule, boolean digitalRule) {
        int hlIndex = Rule.HLH[h][j];
        if (digitalRule) {
            int d = aRule.getHLValueDigital(hlIndex);
            return d;
        } else {
            return -1;
        }
    }
	public String getStringKeyValue(int h, int j, Rule aRule, boolean digitalRule) {
		int hlIndex = Rule.HLH[h][j];
		if (digitalRule) {
			int d = getDigitalKeyValue(h, j, aRule, digitalRule);
	        String key = ctx.getDictionary().getStringValueFor(d);
	        return key;
		} else{ 
			return aRule.getHLValue(hlIndex);
		}
	}
    
    public static String printRuleOnlyMeaningKey(Rule aRule, HLVector vector) {
        for (int h=0;h<Rule.HLIS.length;h++) {
            if (vector.getValue(h)!=0) {
                String v = aRule.getHLValue(Rule.HLH[h][vector.getValue(h)]);
                return v;
            }
        }
        return null;
    }
    
    
    public String printRuleAccordingToVectorLongOld(Rule aRule, HLVector vector) {
        StringBuffer sb = new StringBuffer();
        for (int h=0;h<Rule.HLIS.length;h++) {
            for (int i=0;i<=vector.getValue(h);i++) {
                int hlIndex = Rule.HLH[h][i];
                String v = aRule.getHLValue(hlIndex);
                sb.append(v+" ");
            }
        }
        return sb.toString();
    }
    
    public void parseRuleDigitalDigest(String digest, String delim) {
    	
    }
    
    public static String createRuleDigitalDigest(Rule aRule, HLVector aVector,String delim) {
	    StringBuffer sb = new StringBuffer();
	    for (int h=0;h<Rule.HLIS.length;h++) {
	    	if (h>0) {
	    		sb.append(delim);
	    	}
            int v = aRule.getHLValueDigital(Rule.HLH[h][aVector.getValue(h)]);
            sb.append(""+v);
        }
	    return sb.toString();
    }

	public String printRuleAccordingToVectorFull(Rule aRule, boolean digitalRule) {
	    StringBuffer sb = new StringBuffer();
	    for (int h=0;h<Rule.HLIS.length;h++) {
	    	for (int j = 1; j < Rule.HLH[h].length; j++) {
	            String v = getStringKeyValue(h,j, aRule, digitalRule);
	            sb.append(v);
                sb.append(" ");
			}
            sb.append("\t");
            if (digitalRule) {
                for (int j = 1; j < Rule.HLH[h].length; j++) {
                    String v = getStringKeyValue(h,j, aRule, digitalRule);
                    int d = getDigitalKeyValue(h,j, aRule, digitalRule);
                    sb.append("("+d+")");
                    sb.append(" ");
                }
            }
            sb.append("\n");
	    }
	    sb.append(getStringAttrValue(aRule, digitalRule)+"\n");
	    return sb.toString();
	}
}
